package system.core;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import common.customer.bean.Customer;

public class CheckLoginInterceptor extends AbstractInterceptor {
    /**
	 * 自动生成的version id
	 */
	private static final long serialVersionUID = 1L;
	public static final String LOGIN_KEY = "user";
    public static final String LOGIN_PAGE = "notlogin";
 
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        // 对LoginAction不做该项拦截
        Object action = actionInvocation.getAction();
        if (action instanceof common.customer.action.LoginAction) {
            System.out.println("exit check login, because this is login action.");
            return actionInvocation.invoke();
        }
        // 确认Session中是否存在LOGIN
        Map<String,Object> session = actionInvocation.getInvocationContext().getSession();
        Customer customer = (Customer)session.get(LOGIN_KEY);
        if (customer!=null) {
            // 存在的情况下进行后续操作。
            return actionInvocation.invoke();
        } else {
            // 否则终止后续操作，返回LOGIN
            System.out.println("no login, forward login page!");
            return LOGIN_PAGE;
        }
    }
}